package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.AttachmentVo;
import com.ruoyi.system.domain.vo.TopicVo;
import com.ruoyi.system.mapper.CourseTopicMapper;
import com.ruoyi.system.mapper.SysTopicMapper;
import com.ruoyi.system.mapper.TopicTypeMapper;
import com.ruoyi.system.service.TopicTypeService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 题目类型表(TopicType)表服务实现类
 *
 * @author makejava
 * @since 2024-07-28 10:24:58
 */
@Service("topicTypeService")
public class TopicTypeServiceImpl implements TopicTypeService {
    @Resource
    private TopicTypeMapper topicTypeMapper;

    @Resource
    private CourseTopicMapper courseTopicMapper;

    @Resource
    private SysTopicMapper sysTopicMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TopicType queryById(Long id) {
        return this.topicTypeMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param topicType   筛选条件
     * @return 查询结果
     */
    @Override
    public List<TopicType> queryByPage(TopicType topicType) {
        return (this.topicTypeMapper.queryAllByLimit(topicType));
    }

    /**
     * 新增数据
     *
     * @param topicType 实例对象
     * @return 实例对象
     */
    @Override
    public TopicType insert(TopicType topicType) {
        this.topicTypeMapper.insert(topicType);
        return topicType;
    }

    /**
     * 修改数据
     *
     * @param topicType 实例对象
     * @return 实例对象
     */
    @Override
    public TopicType update(TopicType topicType) {
        this.topicTypeMapper.update(topicType);
        return this.queryById(topicType.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.topicTypeMapper.deleteById(id) > 0;
    }

    @Override
    public List<TopicVo> queryAllByCourseId(String courseId) {
        List<String> types = topicTypeMapper.queryAllByCourseId(courseId);
        List<TopicVo> topicVos = new ArrayList<>();
        for (String type : types) {
            List<SysTopic> sysTopics =new  ArrayList<>();
            List<CourseTopic> courseTopics = courseTopicMapper.queryAllByCourseIdAndTopicType(courseId, type);
            for (CourseTopic courseTopic : courseTopics) {
                SysTopic sysTopic = sysTopicMapper.queryById(courseTopic.getTopicId());
                sysTopics.add(sysTopic);
            }
            topicVos.add(new TopicVo(type, sysTopics));
        }
        return topicVos;
    }

}
